home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / c / gcc / crssrc16.zoo / src / tgoto.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-27  |  5.7 KB  |  256 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1982, Fred Fish            *
  4.  *                All Rights Reserved                *
  5.  *                                    *
  6.  *    This software and/or documentation is released for public    *
  7.  *    distribution for personal, non-commercial use only.        *
  8.  *    Limited rights to use, modify, and redistribute are hereby    *
  9.  *    granted for non-commercial purposes, provided that all        *
  10.  *    copyright notices remain intact and all changes are clearly    *
  11.  *    documented.  The author makes no warranty of any kind with    *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular    *
  14.  *    purpose.                            *
  15.  *                                    *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  LIBRARY FUNCTION
  22.  *
  23.  *    tgoto   expand cursor addressing string from cm capability
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    termcap
  28.  *
  29.  *  SYNOPSIS
  30.  *
  31.  *    char *tgoto(cm,destcol,destline)
  32.  *    char *cm;
  33.  *    int destcol;
  34.  *    int destline;
  35.  *
  36.  *  DESCRIPTION
  37.  *
  38.  *    Returns cursor addressing string, decoded from the cm
  39.  *    capability string, to move cursor to column destcol on
  40.  *    line destline.
  41.  *
  42.  *    The following sequences uses one input argument, either
  43.  *    line or column, and place the appropriate substitution
  44.  *    in the output string:
  45.  *
  46.  *        %d    substitute decimal value (in ASCII)
  47.  *        %2    like %d but forces field width to 2
  48.  *        %3    like %d but forces field width to 3
  49.  *        %.    like %c
  50.  *        %+x    like %c but adds ASCII value of x
  51.  *
  52.  *    The following sequences cause processing modifications
  53.  *    but do not "use up" one of the arguments.  If they
  54.  *    act on an argument they act on the next one to
  55.  *    be converted.
  56.  *
  57.  *        %>xy    if next value to be converted is
  58.  *            greater than value of ASCII char x
  59.  *            then add value of ASCII char y.
  60.  *        %r    reverse substitution of line
  61.  *            and column (line is substituted
  62.  *            first by default).
  63.  *        %i    causes input values destcol and
  64.  *            destline to be incremented.
  65.  *        %%    gives single % character in output.
  66.  *
  67.  *  BUGS
  68.  *
  69.  *    Does not implement some of the more arcane sequences for
  70.  *    radically weird terminals (specifically %n, %B, & %D).
  71.  *    If you have one of these you deserve whatever happens.
  72.  *
  73.  */
  74.  
  75. /*
  76.  *    Miscellaneous stuff
  77.  */
  78.  
  79. #include <stdio.h>
  80. #include <string.h>
  81. #include <termcap.h>
  82.  
  83. #ifndef _COMPILE_H
  84. #  include <compiler.h>
  85. #endif
  86.  
  87. #define MAXARGS 2
  88.  
  89. static char *in;        /* Internal copy of input string pointer */
  90. static char *out;        /* Pointer to output array */
  91. static int args[MAXARGS];    /* Maximum number of args to convert */
  92. static int pcount;        /* Count of args processed */
  93. static char output[64];        /* Converted string */
  94. static void process __PROTO((void));
  95.  
  96.  
  97. /*
  98.  *  PSEUDO CODE
  99.  *
  100.  *    Begin tgoto
  101.  *        If no string to process then
  102.  *        Return pointer to error string.
  103.  *        Else
  104.  *        Initialize pointer to input string.
  105.  *        Initialize pointer to result string.
  106.  *        First arg is line number by default.
  107.  *        Second arg is col number by default.
  108.  *        No arguments processed yet.
  109.  *        While there is another character to process
  110.  *            If character is a not a % character then
  111.  *            Simply copy to output.
  112.  *            Else
  113.  *            Process the control sequence.
  114.  *            End if
  115.  *        End while
  116.  *        Return pointer to static output string.
  117.  *        End if
  118.  *    End tgoto
  119.  *
  120.  */
  121.  
  122. char *tgoto(cm,destcol,destline)
  123. char *cm;
  124. int destcol;
  125. int destline;
  126. {
  127.     if (cm == NULL) {
  128.     return("OOPS");
  129.     } else {
  130.     in = cm;
  131.     out = output;
  132.     args[0] = destline;
  133.     args[1] = destcol;
  134.     pcount = 0;
  135.     while (*in != '\0') {
  136.         if (*in != '%') {
  137.         *out++ = *in++;
  138.         } else {
  139.         process();
  140.         }
  141.     }
  142.      *out = 0;
  143.     return(output);
  144.     }
  145. }
  146.  
  147. /*
  148.  *  INTERNAL FUNCTION
  149.  *
  150.  *    process   process the conversion/command sequence
  151.  *
  152.  *  SYNOPSIS
  153.  *
  154.  *    static process()
  155.  *
  156.  *  DESCRIPTION
  157.  *
  158.  *    Processes the sequence beginning with the % character.
  159.  *    Directly manipulates the input string pointer, the
  160.  *    output string pointer, and the arguments.  Leaves
  161.  *    the input string pointer pointing to the next character
  162.  *    to be processed, and the output string pointer pointing
  163.  *    to the next output location.  If conversion of
  164.  *    one of the numeric arguments occurs, then the pcount
  165.  *    is incremented.
  166.  *
  167.  */
  168.  
  169. /*
  170.  *  PSEUDO CODE
  171.  *
  172.  *    Begin process
  173.  *        Skip over the % character.
  174.  *        Switch on next character after %
  175.  *        Case 'd':
  176.  *        Process %d type conversion (variable width).
  177.  *        Reinitialize output pointer.
  178.  *        Break;
  179.  *        Case '2':
  180.  *        Process %d type conversion (width 2).
  181.  *        Reinitialize output pointer.
  182.  *        Break;
  183.  *        Case '3':
  184.  *        Process %d type conversion (width 3).
  185.  *        Reinitialize output pointer.
  186.  *        Break;
  187.  *        Case '.'
  188.  *        Process %c type conversion.
  189.  *        Break;
  190.  *        Case '+':
  191.  *        Process %c type conversion with offset.
  192.  *        Break;
  193.  *        Case '>':
  194.  *        Process argument modification.
  195.  *        Break;
  196.  *        Case 'r':
  197.  *        Process argument reversal.
  198.  *        Break;
  199.  *        Case 'i':
  200.  *        Increment argument values.
  201.  *        Break;
  202.  *        Case '%':
  203.  *        Copy to output, incrementing pointers.
  204.  *        Break;
  205.  *        End switch
  206.  *    End process
  207.  *
  208.  */
  209.  
  210.  
  211. static void process()
  212. {
  213.     int temp;
  214.  
  215.     in++;
  216.     switch(*in++) {
  217.     case 'd':
  218.     sprintf(out,"%d",args[pcount++]);
  219.     out = &output[(int)strlen(output)];    
  220.     break;
  221.     case '2':
  222.     sprintf(out,"%02d",args[pcount++]);
  223.     out = &output[(int)strlen(output)];
  224.     break;
  225.     case '3':
  226.     sprintf(out,"%03d",args[pcount++]);
  227.     out = &output[(int)strlen(output)];
  228.     break;
  229.     case '.':
  230.     *out++ = args[pcount++];
  231.     break;
  232.     case '+':
  233.     *out++ = args[pcount++] + *in++;
  234.     break;
  235.     case '>':
  236.     if (args[pcount] > *in++) {
  237.         args[pcount] += *in++;
  238.     } else {
  239.         in++;
  240.     }
  241.     break;
  242.     case 'r':
  243.     temp = args[pcount];
  244.     args[pcount] = args[pcount+1];
  245.     args[pcount+1] = temp;
  246.     break;
  247.     case 'i':
  248.     args[pcount]++;
  249.     args[pcount+1]++;
  250.     break;
  251.     case '%':
  252.     *out++ = '%';
  253.     break;
  254.     }
  255. }
  256.